<template>
  <div class="app-container">
    <div class="app-container">
      <Find :OriginalFilter="{ static: true }" :formList="formList" @findClicked="findData" @Launchform="Launchform" />

      <el-card style="margin-top: 10px" shadow="always" :body-style="{ padding: '5px' }">
        <h1>结算作业</h1>
        <Find
          @Launchform="Launchform1"
          :OriginalFilter="{ static: true }"
          :searchImg="false"
          style="margin-bottom: 10px"
          :formList="formList1"
          @findClicked="findData2"
        />
        <div>
          <div>
            <h1>厂商资料查询</h1>
            <Table
              @selectValChange="selectedList"
              @inputChange="inputChange"
              @currentRow="clicktogetdata"
              :isLoading="isLoading7"
              :tableHeader="tableHeader"
              :tableData="ManufacturerDataFileKalParmsTableDate"
              :TableHeight="90"
            />
            <PageModule :PageData="{ total }" @pageSizeChange="handleSizeChange" @pageChange="handleCurrentChange" />
          </div>
        </div>
      </el-card>
    </div>
  </div>
</template>
<script lang="ts">
import { defineComponent } from 'vue'
import JMessage from '@/utils/JMessage'
export default defineComponent({
  name: 'ManufacturerDataFileKal'
})
</script>
<script lang="ts" setup>
import { i18n, Jzhcn } from '@/i18n'
import { VendorQuery, VendorClassification, deleteRow, increase } from '@/api/ControlDepartment/MaterialManagement/ManufacturerDataFileKal'
import type { formListType } from '@/components/Find/index.vue'
import fileRequest from '@/utils/fileRequest'
import LoginWork from '@/utils/LoginWork'
import { ElMessage, ElMessageBox } from 'element-plus'

import { ref, onMounted } from 'vue'

// 修改

// 包材查询
let active = ref(true)
const selectedList = (_: any, row: any) => {
  // console.log(_)
  active.value = true
  inputChange(_)
}

const inputChange = async (row: any) => {
  if (active.value) {
    row.vendorType = row.typeName.selectValue.split(' ')[0]
    row.typeName = row.typeName.selectValue.split(' ')[1]
  } else {
    row.vendorType = row.typeName.selectValue.split(' ')[1]
    row.typeName = row.typeName.selectValue.split(' ')[0]
  }

  const result = await LoginWork.put<any, any>('/SdPosition/SdVendorUpdate', row)
  if (result.code === 200) {
    VendorQueryMethods()
    JMessage.success(result.message)
  } else {
    JMessage.error(result.message)
  }
  active.value = false
}

// 增加
const increaseMethodsParms = ref({
  companyId: '',
  contactMobile: '',
  contactPerson: '',
  endTime: '',
  flag: '',
  mainProduct: '',
  pageNum: '',
  pageSize: '',
  phone: '',
  sampleDiscount: '',
  startTime: '',
  vendorAbbr: '',
  vendorAddr: '',
  vendorName: '',
  vendorNo: '',
  vendorType: ''
})

const Launchform1 = (row: any) => {
  increaseMethodsParms.value.vendorType = row[2].selectValue.split(' ')[0]
  increaseMethodsParms.value.vendorNo = row[3].inputValue
  increaseMethodsParms.value.vendorAbbr = row[4].inputValue
  increaseMethodsParms.value.vendorName = row[5].inputValue

  increaseMethodsParms.value.mainProduct = row[6].inputValue
  increaseMethodsParms.value.contactPerson = row[7].inputValue
  increaseMethodsParms.value.contactMobile = row[8].inputValue

  increaseMethodsParms.value.phone = row[9].inputValue
  increaseMethodsParms.value.vendorAddr = row[10].inputValue
  increaseMethodsParms.value.sampleDiscount = row[11].inputValue
}

const findData2 = (row: any) => {
  if (row === '增加') {
    increaseMethods()
  }
  if (row === '清空') {
    clearFormList2()
  }
}

const increaseMethods = async () => {
  try {
    const result = await increase(increaseMethodsParms.value)
    if (result.code === 200) {
      VendorQueryMethods()
      JMessage.success(result.message)
    } else {
      JMessage.error(result.message)
    }
  } catch (error) {
    JMessage.error('增加失败请重试')
  }
}

const clearFormList2 = () => {
  formList1.value.forEach((item: any) => {
    if (item.type === 'input') {
      item.inputValue = ''
    }
    if (item.type === 'select') {
      item.selectValue = ''
    }
    // Add other conditions for different form item types
  })
}

// 删除
const deleteRowPrams = ref()

const clicktogetdata = (row: any) => {
  deleteRowPrams.value = row
}

const deleteRowMethods = async () => {
  try {
    const result = await deleteRow(deleteRowPrams.value)
    if (result.code === 200) {
      VendorQueryMethods()
      JMessage.success(result.message)
    } else {
      JMessage.error(result.message)
    }
  } catch (error) {
    JMessage.error('删除失败')
  }
}

const typeNameList = ref([])
// 厂商分类
const VendorClassificationMethods = async () => {
  try {
    let NewArray: never[] = []
    const result = await VendorClassification()
    NewArray = result.reduce((arr: any, item: any) => {
      if (item.typeNo && item.typeName) {
        // 只添加非空的 typeName 和 typeNo
        arr.push(item.typeNo + ' ' + item.typeName)
      }
      return arr
    }, [])
    formList.value[0].selectOptions = EvaluatesTheArrayReturnValue(NewArray)
    formList1.value[2].selectOptions = EvaluatesTheArrayReturnValue(NewArray)
    typeNameList.value = EvaluatesTheArrayReturnValue1(NewArray)
  } catch (error) {
    JMessage.error('厂商分类查询出错')
  }
}

const EvaluatesTheArrayReturnValue1 = (ArrayList: any) => {
  return ArrayList.map((item: any, index: any) => {
    return {
      value: item,
      label: item
    }
  })
}

const EvaluatesTheArrayReturnValue = (ArrayList: any) => {
  return ArrayList.map((item: any, index: any) => {
    return {
      optionKey: index,
      optionValue: item,
      optionLabel: item
    }
  })
}

const ManufacturerDataFileKalParms = ref({
  companyId: '',
  contactMobile: '',
  contactPerson: '',
  endTime: '',
  flag: '',
  mainProduct: '',
  pageNum: 1,
  pageSize: 40,
  phone: '',
  sampleDiscount: '',
  startTime: '',
  vendorAbbr: '',
  vendorAddr: '',
  vendorName: '',
  vendorNo: '',
  vendorType: ''
})

const Launchform = (row: any) => {
  // ManufacturerDataFileKalParms.value.vendorName = row[0].selectValue.split(' ')[1]
  ManufacturerDataFileKalParms.value.vendorType = row[0].selectValue.split(' ')[0]

  ManufacturerDataFileKalParms.value.vendorNo = row[1].inputValue
  ManufacturerDataFileKalParms.value.vendorAbbr = row[2].inputValue
  ManufacturerDataFileKalParms.value.startTime = row[3].dateValue
  ManufacturerDataFileKalParms.value.endTime = row[4].dateValue
  VendorQueryMethods()
}

const total = ref(0)

const handleSizeChange = (val: number) => {
  ManufacturerDataFileKalParms.value.pageSize = val
  VendorQueryMethods()
}

const handleCurrentChange = (val: number) => {
  ManufacturerDataFileKalParms.value.pageNum = val
  VendorQueryMethods()
}

const isLoading7 = ref(false)

const ManufacturerDataFileKalParmsTableDate = ref([])
const VendorQueryMethods = async () => {
  isLoading7.value = true
  try {
    const result = await VendorQuery(ManufacturerDataFileKalParms.value)
    ManufacturerDataFileKalParmsTableDate.value = result.list.map((item: any) => {
      item.typeName = {
        selectValue: item.typeName + ' ' + item.vendorType || '',
        options: typeNameList
      }

      item.change = 'Y'
      item.disableConfirm = true
      return item
    })
    total.value = result.total
  } catch (error) {
    JMessage.error('厂商资料查询出错')
  }
  isLoading7.value = false
}

onMounted(() => {
  // VendorQueryMethods()

  VendorClassificationMethods()
})

const formList = ref<any>([
  {
    type: 'select',
    selectValue: '',
    selectP: '厂商类别',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'input',
    inputPlaceholder: '厂商代码',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '厂商简称',
    inputValue: ''
  },
  {
    type: 'dateSingle',
    dateLabel: '开始建档日期',
    dateValue: ''
  },
  {
    type: 'dateSingle',
    dateLabel: '结束建档日期',
    dateValue: ''
  },
  {
    type: 'button',
    checkboxLabel: '置空',
    bgColor: '#1890FF'
  },
  {
    type: 'button',
    checkboxLabel: '删除'
  },
  {
    type: 'button',
    checkboxLabel: '导出'
  }
])

const formList1 = ref<any>([
  {
    type: 'button',
    checkboxLabel: '清空'
  },
  {
    type: 'button',
    checkboxLabel: '增加'
  },
  {
    type: 'select',
    selectValue: '',
    selectP: '厂商类别',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'input',
    inputPlaceholder: '厂商代码',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '厂商简称',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '厂商名称',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '主要产品',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '联系人',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '联系人电话',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '账期',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '地址',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '样品折扣',
    inputValue: ''
  }
])

const handleCloseMateriaLKlc3 = (_done?: () => void) => {
  if (deleteRowPrams.value === undefined) {
    return JMessage.warning('请点击需要删除的数据')
  }
  ElMessageBox.confirm('确认要删除该数据吗?')
    .then(async () => {
      deleteRowMethods()
    })
    .catch(() => {})
}

const findData = async (row: any) => {
  if (row === '删除') {
    handleCloseMateriaLKlc3()
  }
  if (row === '导出') {
    const res = await fileRequest.post<any, any>(`/SdPosition/exportSdVendor`, ManufacturerDataFileKalParms.value)
    const link = document.createElement('a') // 利用a标签自定义下载文件名
    let blob = new Blob([res.data]) // 创建Blob对象，设置文件类型
    link.style.display = 'none'
    //设置连接
    link.href = window.URL.createObjectURL(blob)
    const FileName = decodeURI(res.headers['content-disposition'].split(`filename=`)[1])
    link.download = FileName //自定义文件名字
    document.body.appendChild(link)
    //模拟点击事件
    link.click()
    document.body.removeChild(link)
  }
  if (row === '置空') {
    formList.value.forEach((item: any) => {
      if (item.type === 'input') {
        item.inputValue = ''
      }
      if (item.type === 'select') {
        item.selectValue = ''
      }
      if (item.type === 'dateSingle') {
        item.dateValue = ''
      }
      // Add other conditions for different form item types
    })
  }
}

// 表格数据

const tableHeader = ref([
  {
    label: '厂商类别',
    prop: 'typeName',
    type: 'select'
  },
  {
    label: '厂商代码',
    prop: 'vendorNo',
    change: true
  },
  {
    label: '厂商简称',
    prop: 'vendorAbbr',
    change: true
  },
  {
    label: '厂商名称',
    prop: 'vendorName',
    change: true
  },
  {
    label: '主营产品',
    prop: 'mainProduct'
  },
  {
    label: '联系人',
    prop: 'contactPerson'
  },
  {
    label: '联系人电话',
    prop: 'contactMobile'
  },
  {
    label: '账期',
    prop: 'phone'
  },
  {
    label: '地址',
    prop: 'vendorAddr'
  },
  {
    label: '样品折扣',
    prop: 'sampleDiscount'
  },
  {
    label: '建档人员',
    prop: 'sysUser',
    change: true
  },
  {
    label: '建档日期',
    prop: 'sysDate',
    change: true
  }
])
</script>

<style lang="less" scoped>
h1 {
  font-size: 14px;
  margin-bottom: 5px;
}
</style>
